package com.sdy.hospitalguard.web.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sdy.common.model.BizException;
import com.sdy.common.model.Response;
import com.sdy.common.utils.Assert;
import com.sdy.hospitalguard.biz.constants.Constants;
import com.sdy.hospitalguard.biz.dto.ManagerDto;
import com.sdy.hospitalguard.biz.model.Manager;
import com.sdy.hospitalguard.biz.service.ManagerService;
import com.sdy.hospitalguard.biz.utils.PassWordUtil;
import com.sdy.mvc.controller.BaseController;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;

/**
 * @author: cgj
 * @date:2020/6/1
 */

@Slf4j
@Controller
@RequestMapping("/getManager")
@Api(tags = "账号管理")
public class ManagerController extends BaseController {
    @Autowired
    private ManagerService managerService;

    @PostMapping("/delbyId")
    @ResponseBody
    @ApiOperation(value = "根据Id删除用户", response = ManagerDto.class)

    public Response<Boolean> delbyId(Integer id) throws BizException {
        Assert.isNull(id, "id为空！");
        boolean b = managerService.removeById(id);
        return Response.success(b);
    }

    @PostMapping("/saveOne")
    @ResponseBody
    @ApiOperation(value = "新增用户", response = ManagerDto.class)
    public Response<Boolean> saveOne(@RequestBody ManagerDto managerDto) throws BizException {
        Assert.isBlank(managerDto.getName(), "姓名不能为空");
        Assert.isTrue(!managerDto.getPhone().matches(Constants.REGEX_PHONE), "手机号格式错误");
        Assert.isBlank(managerDto.getPassword(), "密码不能为空");
        Assert.isNull(managerDto.getRoleId(), "角色不可为空");
        Assert.isTrue(managerService.lambdaQuery().eq(Manager::getPhone, managerDto.getPhone()).count() > 0, "该账号已注册!");
        Boolean aBoolean = managerService.saveOne(managerDto.setPassword(PassWordUtil.registerEncryption(managerDto.getPassword())).setState(0));
        Assert.notTrue(aBoolean, "新增失败!");
        return Response.success(aBoolean);
    }

    @PostMapping("/updbyId")
    @ResponseBody
    @ApiOperation(value = "根据Id修改", response = ManagerDto.class)
    public Response<Boolean> updbyId(@RequestBody ManagerDto managerDto) throws BizException {
        Assert.isNull(managerDto.getId(), "主键id丢失!");
        boolean update = managerService.lambdaUpdate()
                .eq(Manager::getId, managerDto.getId())
                .set(Manager::getName, managerDto.getName())
                .update();
        Assert.notTrue(update, "更新失败!");
        return Response.success(update);
    }

    @PostMapping("/modifyPassword")
    @ResponseBody
    @ApiOperation(value = "修改密码", response = ManagerDto.class)
    public Response modifyPassword(@RequestBody Manager manager) throws BizException {
        Assert.isNull(manager, "");
        Assert.isBlank(manager.getPassword(), "密码不能为空");
        boolean update = managerService.lambdaUpdate()
                .eq(Manager::getId, manager.getId())
                .set(Manager::getPassword, PassWordUtil.registerEncryption(manager.getPassword()))
                .update();
        Assert.notTrue(update, "修改失败!");
        return Response.success(update);
    }

    @PostMapping("/updStaById")
    @ResponseBody
    @ApiOperation(value = "禁用", response = ManagerDto.class)
    public Response<Boolean> updStaById(@RequestBody @ApiParam(value = "ManagerDto", name = "放入id + state即可") ManagerDto managerDto) throws BizException {
        Assert.isNull(managerDto, "请传入正确的参数!");
        Assert.isNull(managerDto.getId(), "请传入正确的参数id!");
        Assert.isNull(managerDto.getState(), "请传入正确的参数State!");
        Boolean aBoolean = managerService.updStaById(managerDto.getId(), managerDto.getState());
        return Response.success(aBoolean);
    }

    @GetMapping("/pageManager")
    @ResponseBody
    @ApiOperation(value = "账号管理分页数据", response = ManagerDto.class)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "nameAndPhone", value = "姓名"),
            @ApiImplicitParam(dataType = "Integer", name = "inpatientAreaId", value = "病区主键id")
    })
    public Response pageManager(Page page, String nameAndPhone, Integer inpatientAreaId) throws UnsupportedEncodingException {
        if (null != nameAndPhone) {
            nameAndPhone = URLDecoder.decode(nameAndPhone, StandardCharsets.UTF_8.name());
        }
        Page result = managerService.pageManager(page, nameAndPhone, inpatientAreaId);
        return Response.success(result);
    }

}
